// @ts-ignore
/* eslint-disable */
// import { StringGradients } from 'antd/lib/progress/progress';
import { RequestOptionsType } from '@ant-design/pro-utils';
import { notification } from 'antd';
import { request } from 'umi';

/** 获取规则列表 POST /api/list/hosts */
export async function listHostsInfo(
    data?: {
        /** 当前的页码 */
        current?: number;
        /** 页面的容量 */
        pageSize?: number;
    },
    // 加了排序
    sort?: object,
    options?: { [key: string]: any },
) {
    // console.log('GET API request:/api/list/hosts,value=', data)
    return request<API.HostsList>('/v1/host/filter', {
        method: 'POST',
        // requestType: 'json',
        params: {
            ...data,
            sort,
        },
        ...(options || {}),
        // 通过errorHandler捕获500等网络错误
        errorHandler: ((error: any) => {
            console.log(error);
            notification.error({
                description: "Network Error",
                message: "请求后台数据错误",
            })
        }),
    })
}

/** 添加host POST /api/add/host */
export async function addHost(
    data?: API.HostsInfoListItem,
    options?: { [key: string]: any },
) {
    console.log('PUT API request:/v1/host,value=', data)
    return request<API.HostsList>('/v1/host', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            ...data,
        },
        ...(options || {}),
    });
}

/** 获取规则列表 GET /api/update/host */
export async function updateHost(
    data?: API.HostsInfoListItem,
    options?: { [key: string]: any },
) {
    return request<API.HostsList>('/v1/host', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            ...data,
        },
        ...(options || {}),
    });
}

/** 删除host(软删除) POST /api/remove/host */
export async function removeHost(
    data?: API.HostsInfoListItem,
    options?: { [key: string]: any },
) {
    console.log('DELETE API request:/v1/host,value=', data)
    return request<API.HostsList>('/v1/host', {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            ...data,
        },
        ...(options || {}),
    });
}


/** 获取app列表 POST /api/list/apps */
export async function listAppsInfo(
    data?: {
        /** 当前的页码 */
        current?: number;
        /** 页面的容量 */
        pageSize?: number;
    },
    // 加了排序
    sort?: object,
    options?: { [key: string]: any },
) {
    // console.log('POST API request:/api/list/hosts,value=', data)
    return request<API.AppsList>('/v1/application/filter', {
        method: 'POST',
        requestType: 'json',
        data: {
            ...data,
            sort,
        },
        ...(options || {}),
        // 通过errorHandler捕获500等网络错误
        errorHandler: ((error: any) => {
            console.log(error);
            notification.error({
                description: "Network Error",
                message: "请求后台数据错误",
            })
        }),
    })
}

/** 添加app PUT /v1/application
 * 
 * 
*/
export async function addApp(
    data?: API.AppsInfoListItem,
    options?: { [key: string]: any },
) {
    console.log('PUT API request:/v1/application,value=', data)
    return request<API.AppsInfoListItem>('/v1/application', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            ...data,
        },
        ...(options || {}),
    });
}

/** 获取app POST /api/update/app */
export async function updateApp(
    data?: API.AppsInfoListItem,
    options?: { [key: string]: any },
) {
    return request<API.HostsList>('/api/update/app', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            ...data,
        },
        ...(options || {}),
    });
}

/** 删除app(软删除) POST /api/remove/app */
export async function removeApp(
    data?: API.AppsInfoListItem,
    options?: { [key: string]: any },
) {
    console.log('POST API request:/api/remove/app,value=', data)
    return request<API.HostsList>('/api/remove/app', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        data: {
            ...data,
        },
        ...(options || {}),
    });
}

/** 添加app POST /api/list/apptype 
 * 
 * 返回的值，转换成<RequestOptionsType>类型，显示出来
*/
export async function listAppType(
    options?: { [key: string]: any },
) {
    console.log('POST API request: /api/list/apptype')
    return request<RequestOptionsType>(' /api/list/apptype', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        ...(options || {}),
    })
        // 把获取的数据，转换成 <RequestOptionsType> 对象
        .then(function (response) {
            let apptype_ops = response.data.map(function (oldArr: any) {
                var newObj = {};
                newObj["key"] = oldArr.apptype;
                newObj["value"] = oldArr.apptype;
                newObj["label"] = oldArr.apptype;
                return newObj;
            });
            console.log('转换成的apptype数组为: value=', apptype_ops);
            return apptype_ops;
        });
}

/** 添加app POST /api/list/apptype 
 * 
 * 返回的值，分页未完成
*/
export async function listAppTypeList(
    options?: { [key: string]: any },
) {
    console.log('GET API request: /v1/application/type')
    return request<RequestOptionsType>(' /v1/application/type', {
        method: 'GET',
        // headers: {
        //     'Content-Type': 'application/json',
        // },
        ...(options || {}),
    })
}

/** 获取规则列表 GET /v1/middleware/{type}/info */
export async function getMiddlewareCfgInfo(
    params: {
        // query
        /** 手机号 */
        type?: string;
    },
) {
    let query_url = '/v1/middleware/' + params.type + '/info';
    console.log("event= API.getMiddlewareCfgInfo,query url = ", query_url)
    return request<API.MddilewareCfgInfo>(query_url, {
        method: 'GET',
        params: {
            ...params,
        },
    });
}

/** 获取规则列表 POST /api/list/hosts */
export async function listAppCfgList(
    params: {
        id?: number;
    },
) {
    let query_url = '/v1/application/config/app/' + params.id;
    console.log("event= API.listAppCfgList,query url = ", query_url)
    // curl -X GET "http://172.16.120.91:2020/v1/template/config/app/3" -H "accept: application/json"
    return request<API.AppCfgFileList>(query_url, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        },
        // 通过errorHandler捕获500等网络错误
        errorHandler: ((error: any) => {
            console.log(error);
            notification.error({
                description: "Network Error",
                message: "请求后台数据错误",
            })
        }),
    })
}


/** 获取规则列表 GET /v1/middleware/{type}/info */
export async function getAppCfgInfo(
    params: {
        // query
        app_id?: number,
        config_template_id?: number,
        configtype?: string,
    },
) {
    // curl -X GET "http://172.16.120.91:2020/v1/config?app_id=1&config_template_id=1" -H "accept: application/json"
    let query_url = '';
    params.configtype === 'local' ? query_url = '/v1/config/local' : query_url = '/v1/config/nacos'
    console.log("event= API.getMiddlewareCfgInfo,query url = ", query_url)
    return request<API.MddilewareCfgInfo>(query_url, {
        method: 'GET',
        params: {
            ...params,
        },
    });
}

/** 获取规则列表 GET /v1/middleware/{type}/info */
export async function getAppCfgTemplateInfo(
    params: {
        // query
        app_id?: number,
        config_template_id?: number,
        configtype?: string,
    },
) {
    // curl -X GET "http://172.16.120.91:2020/v1/config?app_id=1&config_template_id=1" -H "accept: application/json"
    let query_url = '';
    params.configtype === 'local' ? query_url = '/v1/template/config/id/' + params.config_template_id : query_url = '/v1/template/config/nacos'
    console.log("event= API.getAppCfgTemplateInfo,query url = ", query_url)
    return request<API.MddilewareCfgInfo>(query_url, {
        method: 'GET',
        params: {
            ...params,
        },
    });
}
